import MsgProto from './MsgProto_pb'
import {guid} from '../Guid'

MsgCenter.MsgType = {
    'TEXT': 0,
    'IMAGE': 1,
    'VOICE': 2,
    'MEDIA': 3,
    'CONNECT': 4,
    'KEEPALIVE': 5,
    'FRIEND': 6,
    'SIGNED': 7
}

MsgCenter.ChatType = {
    'GROUP': 0,
    'SINGLE': 1
}

function MsgCenter() {


}

/**
 * 创建消息
 */
MsgCenter.createMsg = (type, chatType, fromUserId, toUserId, content) => {
    let msgEntity = new MsgProto.MsgEntity();
    msgEntity.setType(type);
    msgEntity.setChattype(chatType);
    msgEntity.setFromuserid(fromUserId);
    msgEntity.setTouserid(toUserId);
    msgEntity.setContent(content);
    msgEntity.setGuid(guid())
    // msgEntity.setCreatetime(new Date())
    return msgEntity;
}

/**
 * 连接消息
 * @param chatType
 * @param fromUserId
 * @param toUserId
 * @param content
 * @returns {MsgProto.MsgEntity}
 */
MsgCenter.createConnectMsg = (userId) => {
    let msgEntity = MsgCenter.createMsg(MsgCenter.MsgType.CONNECT, null, userId, null, "userId:"+userId+": I am online")
    return msgEntity
}

/**
 * 心跳消息
 * @param chatType
 * @param fromUserId
 * @param toUserId
 * @param content
 * @returns {MsgProto.MsgEntity}
 */
MsgCenter.createHeartMsg = (userId) => {
    let msgEntity = MsgCenter.createMsg(MsgCenter.MsgType.KEEPALIVE, null, null, null, "userId:"+userId+"I am alive")
    return msgEntity
}


/**
 * 文本消息
 * @param chatType
 * @param fromUserId
 * @param toUserId
 * @param content
 * @returns {MsgProto.MsgEntity}
 */
MsgCenter.createTextMsg = (chatType, fromUserId, toUserId, content) => {
    let msgEntity = MsgCenter.createMsg(MsgCenter.MsgType.TEXT, chatType, fromUserId, toUserId, content)
    console.log('创建的文本消息是:'+JSON.stringify(msgEntity.toObject()))
    return msgEntity
}




/**
 * 将消息保存到Map
 * msgMap 全局保存的map对象
 * msgObject 接收到的消息
 * 返回一个map消息集合字符
 */
MsgCenter.pushToMap = (chatUserId,msgObject,msgMap,nowChatUserId) => {
    msgMap = JSON.parse(msgMap)
    let msgRecord = msgMap[chatUserId]
    if(!msgRecord){
        msgRecord = new MsgRecord()
    }
    if(chatUserId!=nowChatUserId){ //没有正在聊天
        msgRecord.not_read_count ++;
    }
    msgRecord.msgArray.push(msgObject);
    msgMap[chatUserId] = msgRecord
    return JSON.stringify(msgMap)
}



/**
 * 找出聊天对象
 * @param userId 自己的ID
 */
MsgCenter.getChatUserId = (userId,msgObject) => {
    if(msgObject.chattype == MsgCenter.ChatType.GROUP){ //群聊返回touserid
        return msgObject.touserid;
    }else {
        return msgObject.fromuserid==userId?msgObject.touserid:msgObject.fromuserid
    }
}

/**
 * 清除某个用户的未读取数量
 * @param chatUserId
 * @param msgMap
 */
MsgCenter.clearChatUserMsgRecord = (chatUserId,msgMap)=>{
    msgMap = JSON.parse(msgMap)
    let msgRecord = msgMap[chatUserId]
    if(msgRecord){
        msgRecord.not_read_count = 0
    }
    return JSON.stringify(msgMap);
}

MsgCenter.deserializeBinary = (msgBuff) => {
    return MsgProto.MsgEntity.deserializeBinary(msgBuff)
}

MsgCenter.serializeBinary = (msgEntity) => {
    return msgEntity.serializeBinary()
}


/***消息记录封装***/

function MsgRecord() {
    this.not_read_count = 0
    this.msgArray = []
}

MsgRecord.prototype = {
    not_read_count:null,
    msgArray:null
}



export default MsgCenter
